

/*************本文件用于处理电影页面的reducer数据**************** */


/********************处理数据操作******************************** */

/**电影页面 数据初始化 */
export function data_init(state, action) {
    var newState = Object.assign({}, state , action.initData);

    return newState;
}


/** 处理新增电影 */
export function data_add(state, action) {

    var newState = Object.assign({}, state);
    let newMovie = {
        key: parseInt(state.data[0].key) + 1,
        name: action.form.name,
        synopsis: action.form.synopsis
    }
    newState.data.splice(0, 0, newMovie)
    return newState;
}



/**修改电影 */
export function data_updata(state, action) {

    var newState = Object.assign({}, state);
    var data = newState.data;
    for (var index = 0; index < data.length; index++) {

        var element = data[index];

        if (element.key == action.form.key) {
            data[index] = {
                key: action.form.key,
                name: action.form.name,
                synopsis: action.form.synopsis
            }
        }
    }
    return newState;
}


/**删除电影 */
export function data_remo(state, action) {
    var newState = Object.assign({}, state);
    var data = newState.data;
    var num;
    for (var index = 0; index < data.length; index++) {

        var element = data[index];

        if (element.key == action.index) {
            num = index;
        }
    }
    data.splice(num, 1);
    return newState;
}



export function data_load(state, action) {
    var newState = Object.assign({}, state);
    newState.load= !newState.load;
    return newState;
}